app.controller('BusRouteSortCtrl', function ($scope, $modalInstance, param, ajax, $timeout, toaster, $modal) {

	$scope.param = param;
	if ($scope.param.data == null) $scope.param.data = {};

	$scope.route = $scope.param.route;
	$scope.list = [];
	if ($scope.param.list && $scope.param.list.length > 1) {
		$scope.list = $scope.param.list;
		for (var i in $scope.list) {
			var each = $scope.list[i];
			each.duration = each.duration / 60;
		}
	}

	$scope.destination = function (station) {
		station.destination = !station.destination;

		var ul = angular.element(document).find("li.list-group-item");
		var first = angular.fromJson(angular.element(ul[0]).attr("value"));
		var last = angular.fromJson(angular.element(ul[ul.length - 1]).attr("value"));
		if (station.station.id == first.station.id) station.destination = false;
		if (station.station.id == last.station.id) station.destination = true;
	};

	$scope.moveout = function ($event) {
		var ul = angular.element(document).find("li.list-group-item");
		if (ul.length <= 2) {
			toaster.pop("warning", "无法移除", "线路必须至少有一个始发站和一个终点站。");
			return;
		}
		angular.element($event.currentTarget.parentElement.parentElement.parentElement).remove();
	};

	$scope.ok = function () {
		if ($scope.myForm.$valid) {
			var ids = [];
			var stations = [];
			var destFlag = false;
			var ul = angular.element(document).find("li.list-group-item");
			if (ul.length < 2) {
				toaster.pop("warning", "提醒", "线路必须至少有一个始发站和一个终点站。");
				return;
			}

			for (var idx = 0; idx < ul.length; idx++) {
				var li = angular.element(ul[idx]);
				var station = angular.fromJson(li.attr("value"));
				var id = station.station.id;
				if (ids.indexOf(id) >= 0) {
					toaster.pop("warning", "提醒", "站点有重复，请删除重复的站点。");
					return;
				}

				if (idx == 0) {
					if (station.station.cityId != $scope.route.fromCityId) {
						toaster.pop("warning", "提醒", "始发站所属城市与线路定义不一致。");
						return;
					}
					station.duration = 0;
					station.distance = 0;
					station.type = 1;
					station.destination = false;
				} else if (idx == ul.length - 1) {
					if (station.station.cityId != $scope.route.toCityId) {
						toaster.pop("warning", "提醒", "终点站所属城市与线路定义不一致。");
						return;
					}
					station.type = 3;
					station.destination = true;
				} else {
					station.type = 2;
				}

				if (idx > 0 && station.duration <= 0) {
					toaster.pop("warning", "提醒", "请填写正确的到站用时。");
					return;
				}
				if (destFlag && !station.destination) {
					toaster.pop("warning", "提醒", "上下车站点有交叉，请检查站点的上/下标志。");
					return;
				}

				ids.push(id);
				destFlag = station.destination;
				station.distance = station.distance;
				station.duration = station.duration * 60;
				stations.push({
					id: station.id || 0,
					station: null,
					route: $scope.route,
					stationId: station.station.id,
					distance: station.distance,
					destination: station.destination || false,
					duration: station.duration,
					idx: idx + 1,
					lastIdx: $scope.list.length,
					type: station.type
				});
			}
			$modalInstance.close(stations);
		}
	};

	$scope.add = function () {
		$modal.open({
			templateUrl: 'template/bus/route/addStation.html?v=dee62ade88',
			controller: 'BusOrderAddStationCtrl',
			size: 'md',
			resolve: {
				param: function () {
					return {
						"route": $scope.route
					};
				}
			}
		}).result.then(function (param) {
			if (!param) return;
			var list = [];
			var ul = angular.element(document).find("li.list-group-item");
			for (var i = 0; i < ul.length; i++) {
				list.push(angular.fromJson(angular.element(ul[i]).attr("value")));
			}
			for (var idx in param) {
				param[idx].station = {
					id: param[idx].id,
					address: param[idx].address,
					cityId: param[idx].cityId
				};
				param[idx].distance = 0;
				param[idx].duration = 0;
				param[idx].idx = 0;
				param[idx].type = 2;
				param[idx].destination = true;
				list.push(param[idx]);
			}
			$scope.list = list;
			$timeout(function () {	// refresh sortable after ng-repeat done!!!
				$("ul#list").sortable('destroy');
				$("ul#list").sortable();
			}, 50);
		});
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};
});

app.controller('BusOrderAddStationCtrl', function ($scope, $modalInstance, param, ajax) {

	$scope.param = param;

	$scope.stationlist = [];
	$scope.multiSelected = { stationlist: [] };
	ajax.call(new Req.GET({
		resource: 'station/bs/list'
	})).then(function (result) {
		$scope.stationlist = result.result;
	}, function () { });

	$scope.ok = function () {
		$modalInstance.close($scope.multiSelected.stationlist);
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};
});